package cn.webdai.service.impl;

import cn.webdai.dao.UserMapper;
import cn.webdai.pojo.User;
import cn.webdai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 戴金华
 * @date 2019-12-04 21:45
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 数据格式 大key   小key  值
     * @return
     */
    @Override
    public List<User> findAll() {
        List<User> users = redisTemplate.boundHashOps("USER").values();
        if (users==null||users.size()==0){
            //如果缓存中不存在数据 从数据库中去
            users = userMapper.selectAll();
            redisTemplate.boundHashOps("USER").put("user_findAll",users);
        }
        return users;
    }
}
